package com.returnsoft.softphone.eao;

import java.io.Serializable;
import java.util.Date;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.returnsoft.softphone.entity.Agent;
import com.returnsoft.softphone.entity.PeerState;
import com.returnsoft.softphone.entity.Server;
import com.returnsoft.softphone.exception.EaoExcepcion;
@LocalBean
@Stateless
public class PeerStateEao implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@PersistenceContext(name = "CCenterEJB", unitName = "CCenterEJB")
	private EntityManager em;
	
	public void updateAgent(String host, Integer serverId, Integer agentId) throws EaoExcepcion{
		try {
			
			System.out.println("ingreso a updateAgent");
			
			PeerState peerState = findByHost(host, serverId);
			
			Agent agent = em.find(Agent.class, agentId);
			peerState.setAgent(agent);
			em.merge(peerState);
			em.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	public Integer deleteStatus(String peerName, Integer serverId) throws EaoExcepcion{
		try {
			System.out.println(peerName);
			System.out.println(serverId);
			String query = "DELETE FROM PeerState ps WHERE ps.name= :peerName AND ps.server.id= :serverId";
			Query q = em.createQuery(query);
			
			q.setParameter("peerName", peerName);
			q.setParameter("serverId", serverId);
			Integer result = q.executeUpdate();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	
	public PeerState add(PeerState peerState) throws EaoExcepcion {
		try {
			peerState = em.merge(peerState);
			em.flush();
			return peerState;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}

	}
	
	
	public PeerState findByHost(String host, Integer serverId) throws EaoExcepcion {
		try {
			System.out.println("el host:"+host);
			System.out.println("el serverId:"+serverId);
			Query q = em.createQuery("SELECT ps.id,ps.name,ps.state,ps.number,ps.createdAt FROM PeerState ps join ps.server s where ps.host=:host and s.id=:serverId order by ps.id desc");
			q.setParameter("host", host);
			q.setParameter("serverId", serverId);
			q.setMaxResults(1);
			Object[] peerStateObject = (Object[]) q.getSingleResult();
			
			Integer id = (Integer) peerStateObject[0];
			String name = (String) peerStateObject[1];
			String state = (String) peerStateObject[2];
			String number = (String) peerStateObject[3];
			Date createdAt = (Date) peerStateObject[4];
			
			PeerState peerState = new PeerState();
			peerState.setId(id);
			peerState.setCreatedAt(createdAt);
			peerState.setHost(host);
			peerState.setName(name);
			peerState.setNumber(number);
			peerState.setState(state);
			Server server = new Server();
			server.setId(serverId);
			peerState.setServer(server);
			
			
			return peerState;
		} catch (NoResultException e) {
			return null;
		}  catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	public PeerState findByAgent(String host, Integer agentId) throws EaoExcepcion {
		try {
			
			Query q = em.createQuery("SELECT ps.id,ps.name,ps.state,ps.number,ps.createdAt,s.id "
					+ "FROM PeerState ps left join ps.agent a left join ps.server s "
					+ "where a.id=:agentId and ps.host=:host order by ps.id");
			q.setParameter("agentId", agentId);
			q.setParameter("host", host);
			q.setMaxResults(1);
			Object[] peerStateObject = (Object[]) q.getSingleResult();
			
			Integer id = (Integer) peerStateObject[0];
			String name = (String) peerStateObject[1];
			String state = (String) peerStateObject[2];
			String number = (String) peerStateObject[3];
			Date createdAt = (Date) peerStateObject[4];
			Integer serverId = (Integer) peerStateObject[5];
			
			PeerState peerState = new PeerState();
			peerState.setId(id);
			peerState.setCreatedAt(createdAt);
			peerState.setHost(host);
			peerState.setName(name);
			peerState.setNumber(number);
			peerState.setState(state);
			Server server = new Server();
			server.setId(serverId);
			peerState.setServer(server);
			
			
			return peerState;
		} catch (NoResultException e) {
			return null;
		}  catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	
	public PeerState find(Integer serverId, String peerName) throws EaoExcepcion {
		try {
			
			System.out.println("el serverId:"+serverId);
			System.out.println("el peerName:"+peerName);
			Query q = em.createQuery("SELECT ps.id,ps.name,ps.state,ps.number,ps.createdAt,ps.host, "
					+ "a.id "
					+ "FROM PeerState ps "
					+ "left join ps.server s "
					+ "left join ps.agent a "
					+ "where s.id=:serverId and ps.name=:peerName order by ps.id desc");
			q.setParameter("peerName", peerName);
			
			q.setParameter("serverId", serverId);
			q.setMaxResults(1);
			Object[] peerStateObject = (Object[]) q.getSingleResult();
			
			Integer id = (Integer) peerStateObject[0];
			String name = (String) peerStateObject[1];
			String state = (String) peerStateObject[2];
			String number = (String) peerStateObject[3];
			Date createdAt = (Date) peerStateObject[4];
			String host = (String) peerStateObject[5];
			Integer agentId = (Integer) peerStateObject[6];
			
			PeerState peerState = new PeerState();
			peerState.setId(id);
			peerState.setCreatedAt(createdAt);
			peerState.setHost(host);
			peerState.setName(name);
			peerState.setNumber(number);
			peerState.setState(state);
			Server server = new Server();
			server.setId(serverId);
			peerState.setServer(server);
			Agent agent = new Agent();
			agent.setId(agentId);
			peerState.setAgent(agent);
			System.out.println("retorna el peerState");
			return peerState;
		} catch (NoResultException e) {
			return null;
		}  catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	

}
